home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
KOSCHEKA
/
FILEPEEK.C
next >
Wrap
C/C++ Source or Header
|
1990-06-07
|
4KB
|
153 lines
/********************************/
/* File: FilePeek.c */
/* */
/* Given the sector index into */
/* an existing file, read the */
/* sector in and "dump" it to */
/* the screen */
/* */
/* Paramters: */
/* param0 = file reference num */
/* ( file is open ) */
/* param1 = sector number */
/* (1 sector = 512 bytes) */
/* ---------------------------- */
/* To Build: */
/* */
/* (1) Create a project using */
/* this file as well as the */
/* XCMD.Glue.c file. (Set */
/* project type to XCMD (or */
/* XFCN) from the Project menu. */
/* */
/* (2) Bring the project up to */
/* date. */
/* */
/* (3) Build Code Resource. */
/* */
/* (4) Use ResEdit to copy the */
/* resource to your stack. */
/********************************/
#include <MacTypes.h>
#include <OSUtil.h>
#include <MemoryMgr.h>
#include <FileMgr.h>
#include <ResourceMgr.h>
#include <pascal.h>
#include <strings.h>
#include "HyperXCmd.h"
#include "HyperUtils.h"
#define SECTOR 512 /*** size of input buffer ***/
#define NUMROWS 8 /*** across each line ***/
#define NUMLINES 32 /*** lines of data ***/
#define NUMBYTES 16 /*** #bytes per row of data ***/
#define SPACE 0x020 /*** the space character ***/
pascal void main( paramPtr )
XCmdBlockPtr paramPtr;
{
short err;
short fref;
short lc; /* line count */
short rc; /* row count */
long cnt;
long blk; /* sector number */
Handle outData;
short *rPtr; /* per row */
char *cPtr; /* for ASCII */
char *aPtr; /* points to asciival */
char *buf;
char asciiStr[32];
char curntLine[256];
Str31 numString;
outData = 0L;
if( paramPtr->paramCount == 2 ){ /*** expect two parameters ***/
/*** (1) Get our input parameters ***/
fref = (short)paramtoNum( paramPtr, 0 );
blk = paramtoNum( paramPtr, 1 );
/*** (2) Read a buffer of data ***/
blk = blk * SECTOR;
err = SetFPos( fref, fsFromStart, blk );
if ( !err ){
cnt = SECTOR;
/*** We need to keep this data locked ***/
/*** Since we can;t trust callbacks not ***/
/*** to move stuff, we allocate the ***/
/*** buffer as non-relocatable. ***/
buf = NewPtr( cnt );
err = FSRead( fref, &cnt, buf );
if ( err != noErr && err != eofErr ){
paramPtr->returnValue = 0L;
return;
}
}
/*** result is returned to hypercard as ***/
/*** a null terminated string ***/
outData = NewHandle( 0L );
/*** (3) Start filling the output buffer ***/
*curntLine = '\0';
/*** first the number of bytes read in ***/
NumToHex( paramPtr, cnt, 4 , &numString );
appendChar( PtoCstr( (char *)&numString), '\r' );
CopyStrToHandle( &numString, outData );
/*** point to the input data ***/
rPtr = (short *)buf;
for( lc = 0; lc < NUMLINES; ++lc ){
*curntLine = '\0';
/*** blk is the sector address ***/
NumToHex( paramPtr, blk, 6, &numString );
PtoCstr( (char *)&numString );
strcat( curntLine, &numString );
appendChar( curntLine, ':' );
appendChar( curntLine, SPACE );
aPtr = asciiStr;
blk += NUMBYTES;
for( rc = 0; rc < NUMROWS; ++rc ){
/*** convert eight shorts per row ***/
/*** if we overrun the buffer, draw ***/
/*** whatever data follows it... ***/
NumToHex( paramPtr, (long)*rPtr, 4 , &numString);
strcat( curntLine, PtoCstr( (char *)&numString));
appendChar( curntLine, ' ' );
cPtr = (char *)rPtr;
aPtr = CopyAscii( aPtr, *cPtr++ );
aPtr = CopyAscii( aPtr, *cPtr++ );
rPtr++;
}
*aPtr = '\0'; /*** terminate the ascii data ***/
strcat( curntLine, asciiStr );
appendChar( curntLine, '\r' );
/*** move line into output buffer ***/
CopyStrToHandle( curntLine, outData );
}
DisposPtr( buf );
}
cnt = GetHandleSize( outData );
*(*outData + cnt) = '\0';
paramPtr->returnValue = outData;
}